// Copyright 2008-2009 BUGCOLLECT.COM
// Author: Remus Rusanu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//                 http://bugcollect.com/terms
//

package com.bugcollect;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

/**
 * The bugcolect.com report submittion authenticator component.
 * Each <code>{@link ReportingManager}</code> has one <code>ReportAuthenticator</code>
 * instance that is used to authenticate the report submittion. Because the
 * Java framework allows only one global authenticator if your application
 * alos uses an authenticator it needs to restore it after calling <code>
 * {@link ReportingManager#Submit}</code>
 */
final class ReportAuthenticator extends Authenticator {
    private final String applicationName;
    private final String applicationPassword;

    /**
     * Constructs an authenticator with the given credentials
     * @param applicationName
     * @param applicationPassword
     */
    public ReportAuthenticator(
            String applicationName
            , String applicationPassword)
    {
        this.applicationName = applicationName;
        this.applicationPassword = applicationPassword;

    }

    /**
     * Gets the supplied credentials.
     * Called by the Java framework
     * when an HTTP authentication is attempted.
     */
    @Override
    public PasswordAuthentication getPasswordAuthentication () {
        return new PasswordAuthentication (
                applicationName, applicationPassword.toCharArray());
    }
}
